package myJs.marker.ssr

import myJs.Implicits._
import myJs.cps.BootstrapTableCp
import myJs.cps.BootstrapTableCp.Column
import myJs.myPkg.{MyFetch, layer, parse}
import myJs.tool.Pojo._
import myJs.tool.Tool
import shared.Pojo.{CellData, GetFileData}
import slinky.core.FunctionalComponent
import slinky.core.annotations.react
import slinky.core.facade.Hooks._
import slinky.web.html._

import scala.collection.immutable.SeqMap
import scala.concurrent.ExecutionContext.Implicits.global
import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => g}
import upickle.default._
import com.softwaremill.quicklens._

/**
 * Created by yz on 3/11/2020
 */
@react object Ssr2Cp {

  type Props = Unit

  val component = FunctionalComponent[Props] { props =>

    val (datas, setDatas) = useState(List.empty[Map[String,CellData]])
    val (columns, setColumns) = useState(List.empty[Column])

    object FTool {

      def getTableRs = {
        val options = Tool.layerOptions.copy(shade = List("0.1", "#fff"))
        val index = layer.load(1, options)
        val url = g.jsRoutes.controllers.MarkerController.getFileData().url.toString
        MyFetch.getRnStr(url = s"${url}?fileName=SSR.xlsx").map { str =>
          layer.close(index)
         val data=read[GetFileData.RawResponse](str)
          val array=data.array.map{map=>
            map.map{case(k,v)=>
              (k,CellData(value = ujson.Str(v),rowspan = 1))
            }
          }
          GetFileData.Response(
            array = array,
            map = data.map
          )
        }
      }

      def refreshTableB = () => {
        getTableRs.map { response =>
          val curDatas = response.array
          val map = response.map
          val basicColumns = map.map { case (k, v) =>
            val searchKind = v match {
              case "start" | "end" => NumericSearch
              case _ => TextSearch
            }
            val title = v match {
              case _ => v
            }
            val fmt = FTool.tbFmt(v)
            val comVF = FTool.comVFmt(v)
            val optionMap = searchKind match {
              case CheckboxSearch => curDatas.map { dict =>
                dict(k).value.str
              }.distinct.optionMap
              case _ => SeqMap[String, String]()
            }
            Column(field = k, title = title, operable = false, searchKind = searchKind, formatter = fmt, comVF = comVF,
              optionMap = optionMap)
          }.toList
          setColumns(basicColumns)
          val datas = curDatas
          setDatas(datas)
        }
      }

      def tbFmt(fileTitle: String) = (v: CellData, row: Map[String,CellData]) => {
        fileTitle match {
          case _ =>
            span(v.value.myStr)()
        }
      }

      def comVFmt(fileTitle: String) = (v: CellData, row: Map[String,CellData]) => {
        fileTitle match {
          case _ => v.toString
        }
      }

    }

    useEffect(() => {
      FTool.refreshTableB()
    }, List()
    )

    div(
      BootstrapTableCp(columns = columns, datas = datas, pageList = List(10, 25, 50), pageSize = 10,
        selectColumnEnabled = true,pagination = true,
      ),
    )

  }

}



